package util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class TestDataUtil {
    private static Logger logger = LoggerFactory.getLogger(TestDataUtil.class);
    public static Object[][] build_data(String path_name, String keys) throws IOException {
        logger.info("keys={}",keys);
        String file = FileUtils.readFileToString(new File("./data/" + path_name), "utf-8");
        String[] strings = keys.split(",");
        JSONArray jsonArray = JSONArray.parseArray(file);
        Object[][] test_data = new Object[jsonArray.size()][];
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            //一位数组
            Object[] case_data=new Object[strings.length];
            for(int j=0;j<strings.length;j++){
                String key=strings[j];
                Object value = jsonObject.get(key);
                case_data[j]=value;
            }
            test_data[i]=case_data;
        }
        logger.info("test_data={}", Arrays.deepToString(test_data));
        return test_data;
    }
}